﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Gmis.Public;
namespace Gmis.Web.Manage.Village
{
    /// <summary>
    /// Column 的摘要说明
    /// </summary>
    public class Column : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            

            string action = context.Request["Action"].ToLower();
            
            if (string.IsNullOrEmpty(action))
            {
                context.Response.Write(CommonFunction.failure("无指定操作"));
            }
            else
            {
                //ProcessAction();
                switch (action.ToLower())
                {
                    case "add":
                        ProcessAddAction(context);
                        break;
                       
                    case "delete":
                        ProcessDeleteAction(context);
                        break;
                    case "deleteall":
                        //context.Response.Write(DeleteAll(context.Request["ids"]));
                        break;
                    case "edit":
                        ProcessEditAction(context);
                        break;
                }
            }

            //context.Response.Write(CommonFunction.success());
        }

        private void ProcessDeleteAction(HttpContext context)
        {
            //throw new NotImplementedException();
            Gmis.BLL.Column bllColumn = new BLL.Column();
            try
            {
                bllColumn.Delete(context.Request["ColumnId"]);
                context.Response.Write(CommonFunction.success("", "ColumnList"));
            }
            catch
            {
                context.Response.Write(CommonFunction.failure());

            }
        }

        private void ProcessEditAction(HttpContext context)
        {
            //throw new NotImplementedException();
            Gmis.Model.Column columnInfo = GetColumnInfo(context);
            columnInfo.sId = context.Request["ColumnId"];
            columnInfo.SVillage = context.Request["VillageId"];
            Gmis.BLL.Column bllColumn = new BLL.Column();
            try
            {
                bllColumn.Update(columnInfo);
                context.Response.Write(CommonFunction.success("", "ColumnList"));
            }
            catch
            {
                context.Response.Write(CommonFunction.failure());

            }
        }

        private void ProcessAddAction(HttpContext context)
        {
            //throw new NotImplementedException();
            Gmis.Model.Column columnInfo = GetColumnInfo(context);
            columnInfo.SVillage = context.Request["VillageId"];
            Gmis.BLL.Column bllColumn = new BLL.Column();
            try
            {
                bllColumn.Add(columnInfo);
                context.Response.Write(CommonFunction.success("", "ColumnList"));
            }
            catch
            {
                context.Response.Write( CommonFunction.failure());

            }


        }
        private Gmis.Model.Column GetColumnInfo(HttpContext context)
        {
            Gmis.Model.Column columnInfo = new Model.Column();
            columnInfo.sId = CommonFunction.GetGuid();
            columnInfo.SVillage = "";
            columnInfo.lEnable = true;
            columnInfo.nSort = context.Request["nSort"] == "" ? 0 : int.Parse(context.Request["nSort"]);
            columnInfo.sComment = context.Request["sComment"];
            columnInfo.sName = context.Request["sName"];
            columnInfo.sParent = context.Request["sParent"];
            columnInfo.sShort = context.Request["sShort"];
            return columnInfo;
 
        }
       


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        public string success()
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!!!!!!!!!\"}";
        }
    }
}